/*
 * mysql_test.c
 *
 *  Created on: Jun 21, 2015
 *      Author: root
 */

#include <mysql.h>
#include <stdlib.h>
#include <stdio.h>

int main_mysql(void) {
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;

	/* db configure*/

	char *server = "sql6.freemysqlhosting.net";
	char *user = "sql681406";
	char *password = "uY9%sN1%";
	char *database = "sql681406";

	int port = 3306;

	conn = mysql_init(NULL);

	/* Connect to database */

	if (!mysql_real_connect(conn, server, user, password, database, port, NULL, 0)) {
		fprintf(stderr, "connect error: %s\n", mysql_error(conn));
		exit(0);
	}

	if (!mysql_set_character_set(conn, "utf8")) {
	    printf("New client character set: %s\n", mysql_character_set_name(conn));
	}

	/* send SQL query */

	if (mysql_query(conn, "select * from ndb_test")) {
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(0);
	}

	res = mysql_use_result(conn);

	/* output table name */

	printf("MySQL Tables in mysql database:\n");

	while ((row = mysql_fetch_row(res)) != NULL)
		printf("%s - %s \n", row[0], row[1]);

	/* close connection */
	mysql_free_result(res);
	mysql_close(conn);
	return 0;
}

/*

{define {host} as {"sql6.freemysqlhosting.net"}}
{define {user} as {"sql681406"}}
{define {password} as {"uY9%sN1%"}}
{define {database} as {"sql681406"}}
{define {port} as {3306}}
{define {connection} as {init mysql connection}}
{if {not {connect {connection} to database {database} with host {host} and port {port}, where username is {user} and password is {password}}} then
	{write error {"connect error: "} {get mysql error message from connection {connection}} {"\n"}}}
{if {set character set {"utf8"} to connection {connection}} then
	{write error {"New client character set: "} {get character set name from connection {connection}} {"\n"}}}
{if {not {send query sql {"select * from ndb_test"} to connection {connection}}} then
	{write error {get mysql error message from connection {connection}} {"\n"}}}
{define {result} as {get query result from connection {connection}}}
{write error {"MySQL Tables in mysql database:\n"}}
{define function {not-null?}: {{x} is not null?} as {not {{x} is null?}}}
{define {row} as {'()}}
{define function {fetch-data}: {fetch data from {result}} as
	{begin procedure {set {row} as {fetch row from query result {result}}}
		{if {{row} is not null?} then
			{begin procedure {write error {get field value from {row} at column {0}} {get field value from {row} at column {1}}}
				{fetch data from {result}}}}}}
{fetch data from {result}}
{free result {result}}
{close connection {connection}}

 * */
